package com.suning.sawp.web.controller.member;

import java.security.Principal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.suning.sawp.constants.ClerkConstants;
import com.suning.sawp.constants.ErrorCodeConstants;
import com.suning.sawp.constants.LoggerConstants;
import com.suning.sawp.dto.common.ReturnMsg;
import com.suning.sawp.dto.mainpush.StoreManDto;
import com.suning.sawp.intf.storeman.StoreManService;
import com.suning.sawp.member.service.ActivityStatisService;
import com.suning.sawp.member.vo.ActivityStatisVo;
import com.suning.sawp.member.vo.CateStatisDetailVo;
import com.suning.sawp.member.vo.ClientStatisDetailVo;
import com.suning.sawp.member.vo.EmpStatisDetailVo;
import com.suning.sawp.web.controller.DjBaseController;
import com.suning.sawp.web.exception.MyGsonView;
/**
 * 
 * 活动统计控制层<br> 
 *
 * @author 14060886 祝翔龙
 */
@Controller
@RequestMapping("/activity/statis")
public class ActivityStatisController extends DjBaseController {
    
    private static final Logger LOGGER = LoggerFactory.getLogger(LoggerConstants.SAWP_SERVICE);
    
    @Autowired
    private ActivityStatisService activityStatisService;
    @Autowired
    private StoreManService storeManService;
    
    /**
     * 
     * 功能描述: 查询活动统计情况
     *
     * @param cateCode
     * @param employeeId
     * @return
     */
    @RequestMapping("/queryActivityStatis.do")
    public MyGsonView queryActivityStatis(Principal principal, String cateCode, String employeeId, 
            @RequestParam(defaultValue = "0", required = false) String startIndex) {
        LOGGER.info("queryActivityStatis in, principal={}, cateCode={}, employeeId={}", principal.getName(), cateCode, employeeId);
        ReturnMsg<Map<String, Object>> retMsg = new ReturnMsg<Map<String, Object>>();
        // 工号
        String staffId = principal.getName();
        // 查询店员信息
        StoreManDto storeMan = storeManService.queryStoreManAttachInfo(staffId);
        if(null == storeMan){
            // 店员信息异常
            retMsg.setError(ErrorCodeConstants.ERR_MEM_0002);
            return setGsonViewWithEmpty(retMsg);
        }
        String positionId = storeMan.getPositionId();
        String storeCode = storeMan.getStoreCode();
        Map<String, Object> result = new HashMap<String, Object>();
        if (ClerkConstants.STORE_KEEPER.equals(positionId)) {
            List<ActivityStatisVo> activityStatisList = activityStatisService.queryActivityStatisByStore(storeCode, startIndex);
            result.put("activityStatisList", activityStatisList);
        } else if (ClerkConstants.SALE_GUIDE.equals(positionId)) {
            result = activityStatisService.queryActivityStatisByCate(storeMan, cateCode, startIndex);
        } else if (ClerkConstants.SALES_CLERK.equals(positionId) || ClerkConstants.SALES_PROMOTION.equals(positionId)) {
            List<ActivityStatisVo> activityStatisList = activityStatisService.queryActivityStatisByEmp(storeCode, staffId, startIndex);
            result.put("activityStatisList", activityStatisList);
        } else {
            LOGGER.warn("未知的岗位：{},试图查询外拓活动统计信息,被拒绝", positionId);
            retMsg.setError(ErrorCodeConstants.ERR_MEM_0012);
            return setGsonViewWithEmpty(retMsg);// 暂无权限查询
        }
        retMsg.setData(result);
        return setGsonViewWithEmpty(retMsg);
    }
    
    /**
     * 
     * 功能描述: 查询某活动统计详情
     *
     * @return
     */
    @RequestMapping("/queryCateStatisDetailByStore.do")
    public MyGsonView queryCateStatisDetailByStore(Principal principal, String activityId, String source, String mode) {
        LOGGER.info("queryCateStatisDetailByStore in, principal={}, activityId={}", principal.getName(), activityId);
        ReturnMsg<CateStatisDetailVo> retMsg = new ReturnMsg<CateStatisDetailVo>();
        if (isFieldNull(activityId)) {
            return getErrorGsonView(retMsg);
        }
        // 工号
        String staffId = principal.getName();
        // 查询店员信息
        StoreManDto storeMan = storeManService.queryStoreManAttachInfo(staffId);
        if(null == storeMan){
            // 店员信息异常
            retMsg.setError(ErrorCodeConstants.ERR_MEM_0002);
            return setGsonViewWithEmpty(retMsg);
        }
        CateStatisDetailVo cateStatisDetail = activityStatisService.queryCateStatisDetailByStore(storeMan.getStoreCode(), activityId, source, mode);
        retMsg.setData(cateStatisDetail);
        return setGsonViewWithEmpty(retMsg);
    }
    
    /**
     * 
     * 功能描述: 查询某品类统计详情
     *
     * @return
     */
    @RequestMapping("/queryEmpStatisDetailByCate.do")
    public MyGsonView queryEmpStatisDetailByCate(Principal principal, String activityId, String cateCode, String source, String mode) {
        LOGGER.info("queryEmpStatisDetailByCate in, principal={}, activityId={}, cateCode={}", principal.getName(), activityId, cateCode);
        ReturnMsg<EmpStatisDetailVo> retMsg = new ReturnMsg<EmpStatisDetailVo>();
        if (isFieldNull(activityId, cateCode)) {
            return getErrorGsonView(retMsg);
        }
        // 工号
        String staffId = principal.getName();
        // 查询店员信息
        StoreManDto storeMan = storeManService.queryStoreManAttachInfo(staffId);
        if(null == storeMan){
            // 店员信息异常
            retMsg.setError(ErrorCodeConstants.ERR_MEM_0002);
            return setGsonViewWithEmpty(retMsg);
        }
        EmpStatisDetailVo empStatisDetail = activityStatisService.queryEmpStatisDetailByCate(storeMan.getStoreCode(), activityId, cateCode, source, mode);
        retMsg.setData(empStatisDetail);
        return setGsonViewWithEmpty(retMsg);
    }
    
    /**
     * 
     * 功能描述: 查询某店员统计详情
     *
     * @return
     */
    @RequestMapping("/queryClientStatisDetailByEmp.do")
    public MyGsonView queryClientStatisDetailByEmp(Principal principal, String activityId, String cateCode, String employeeId, 
            String source, String mode, @RequestParam(defaultValue = "0", required = false) String startIndex) {
        LOGGER.info("queryClientStatisDetailByEmp in, principal={}, activityId={}, employeeId={}, source={}, mode={}, startIndex={}", 
                principal.getName(), activityId, employeeId, source, mode, startIndex);
        ReturnMsg<ClientStatisDetailVo> retMsg = new ReturnMsg<ClientStatisDetailVo>();
        if (isFieldNull(activityId, employeeId)) {
            return getErrorGsonView(retMsg);
        }
        // 工号
        String staffId = principal.getName();
        // 查询店员信息
        StoreManDto storeMan = storeManService.queryStoreManAttachInfo(staffId);
        if(null == storeMan){
            // 店员信息异常
            retMsg.setError(ErrorCodeConstants.ERR_MEM_0002);
            return setGsonViewWithEmpty(retMsg);
        }
        ClientStatisDetailVo clientStatisDetail = activityStatisService.queryClientStatisDetailByEmp(storeMan.getStoreCode(), activityId, cateCode, 
                employeeId, source, mode, startIndex);
        retMsg.setData(clientStatisDetail);
        return setGsonViewWithEmpty(retMsg);
    }
    
    @SuppressWarnings({ "rawtypes", "unchecked" })
    private MyGsonView getErrorGsonView(ReturnMsg retMsg){
        retMsg.setRetFlag(ReturnMsg.FAIL);
        retMsg.setErrorCode(ErrorCodeConstants.ERR_0001);
        retMsg.setErrorMessage(ErrorCodeConstants.ERR_MAP.get(ErrorCodeConstants.ERR_0001));
        return setGsonViewWithEmpty(retMsg);
    }
    
}
